package com.zbcz.erp.util

import android.view.View
import java.util.concurrent.TimeUnit

/**
 * 防重复点击工具类
 *
 * @author zhang hao
 */
object SingleClickUtils {

    /**
     * Global single click interval.
     */
    var singleClickInterval: Int = 1000
        set(value) {
            if (value <= 0) {
                throw IllegalArgumentException("Single click interval must be greater than 0.")
            }
            field = value
        }

    /**
     * Register a callback to be invoked when this view is single clicked.
     *
     * @param listener Single click listener.
     */
    @JvmStatic
    fun onSingleClick(view: View, listener: View.OnClickListener) {
        view.onSingleClick(listener = listener)
    }

    /**
     * Register a callback to be invoked when this view is single clicked.
     *
     * @param interval Single click interval. Unit is [TimeUnit.MILLISECONDS].
     * @param listener Single click listener.
     */
    @JvmStatic
    fun onSingleClick(view: View, interval: Int, listener: View.OnClickListener) {
        view.onSingleClick(interval, listener = listener)
    }


    @JvmStatic
    fun setOnClickListener(view: View, listener: View.OnClickListener) {
        view.onSingleClick(500, false, listener = listener)
    }

    /**
     * Register a callback to be invoked when this view is single clicked.
     *
     * @param isShareSingleClick True if this view is share single click interval whit other view
     *   in same Activity, false otherwise.
     * @param listener Single click listener.
     */
    @JvmStatic
    fun onSingleClick(view: View, isShareSingleClick: Boolean, listener: View.OnClickListener) {
        view.onSingleClick(isShareSingleClick = isShareSingleClick, listener = listener)
    }

    /**
     * Register a callback to be invoked when this view is single clicked.
     *
     * @param interval Single click interval. Unit is [TimeUnit.MILLISECONDS].
     * @param isShareSingleClick True if this view is share single click interval whit other view
     *   in same Activity, false otherwise.
     * @param listener Single click listener.
     */
    @JvmStatic
    fun onSingleClick(
        view: View,
        interval: Int,
        isShareSingleClick: Boolean,
        listener: View.OnClickListener
    ) {
        view.onSingleClick(interval, isShareSingleClick, listener)
    }

    /**
     * Determine whether to trigger a single click.
     *
     * @param listener Single click listener.
     */
    @JvmStatic
    fun determineTriggerSingleClick(view: View, listener: View.OnClickListener) {
        view.determineTriggerSingleClick(listener = listener)
    }

    /**
     * Determine whether to trigger a single click.
     *
     * @param interval Single click interval.Unit is [TimeUnit.MILLISECONDS].
     * @param listener Single click listener.
     */
    @JvmStatic
    fun determineTriggerSingleClick(view: View, interval: Int, listener: View.OnClickListener) {
        view.determineTriggerSingleClick(interval = interval, listener = listener)
    }

    /**
     * Determine whether to trigger a single click.
     *
     * @param isShareSingleClick True if this view is share single click interval whit other view
     *   in same Activity, false otherwise.
     * @param listener Single click listener.
     */
    @JvmStatic
    fun determineTriggerSingleClick(
        view: View,
        isShareSingleClick: Boolean,
        listener: View.OnClickListener
    ) {
        view.determineTriggerSingleClick(
            isShareSingleClick = isShareSingleClick, listener = listener
        )
    }

    /**
     * Determine whether to trigger a single click.
     *
     * @param interval Single click interval.Unit is [TimeUnit.MILLISECONDS].
     * @param isShareSingleClick True if this view is share single click interval whit other view
     *   in same Activity, false otherwise.
     * @param listener Single click listener.
     */
    @JvmStatic
    fun determineTriggerSingleClick(
        view: View,
        interval: Int,
        isShareSingleClick: Boolean,
        listener: View.OnClickListener
    ) {
        view.determineTriggerSingleClick(interval, isShareSingleClick, listener)
    }
}